<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>班级列表</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/black/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/css/demo.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/js/validateExtends.js"></script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">
</table>

<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left; margin-right: 10px;"><a id="edit-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
    <div style="margin-top: 3px;">班级名称：<input id="clazzName" class="easyui-textbox" name="clazzName" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
    </div>
</div>

<!-- 添加窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td>班级名称:</td>
                <td><input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="name"  data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>班级介绍:</td>
                <td>
                    <textarea id="info" name="info" style="width: 200px; height: 60px;" class="" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- 编辑窗口 -->
<div id="editDialog" style="padding: 10px">
    <form id="editForm" method="post">
        <input type="hidden" id="edit-id" name="id">
        <table cellpadding="8" >
            <tr>
                <td>班级名称:</td>
                <td><input id="edit_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="name"  data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>班级介绍:</td>
                <td>
                    <textarea id="edit_info" name="info" style="width: 200px; height: 60px;" class="" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<script type="text/javascript">
    $('#dataList').datagrid({
        title:'班级列表',
        iconCls:'icon-more',//图标
        border: true,
        collapsible: false,//是否可折叠的
        method: "post",
        fit: false,
        url: "/clazz/list",  ///*[[@{/clazz/getClazzList}]]*/
        idField:'id',
        singleSelect: false,//是否单选
        pagination: true,//分页控件
        rownumbers: true,//行号
        sortName: 'id',
        sortOrder: 'DESC',
        remoteSort: false,
        columns: [
            [
                {field:'chk',checkbox: true,width:50},
                {field:'id',title:'ID',width:50, sortable: true},
                {field:'name',title:'班级名称',width:200},
                {field:'info',title:'班级介绍',width:100,
                },
            ]
        ],
        toolbar: "#toolbar"
    });
    //设置工具类按钮
    $("#add").click(function(){
        $("#addDialog").dialog("open");
    });
    //删除
    $("#delete").click(function(){
        var selectAll = $("#dataList").datagrid("getSelections");
        if(selectAll == null){
            $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
        } else{
            var jsonObj = {};
            $.each(selectAll,function(i,n){
                jsonObj["ids["+i+"]"] = n.id;
            });
            $.messager.confirm("消息提醒", "将删除班级信息（如果班级下存在学生或教师则不能删除），确认继续？", function(r){
                if(r){
                    $.ajax({
                        type: "post",
                        url: "/clazz/delete"/*[[@{/clazz/deleteClazz}]]*/,
                        data: jsonObj,
                        success: function(data){
                            if(data.success){
                                $.messager.alert("消息提醒",data.message,"info");
                                //刷新表格
                                $("#dataList").datagrid("reload");
                            } else{
                                $.messager.alert("消息提醒",data.message,"warning");
                                return;
                            }
                        }
                    });
                }
            });

        }
    });



    //设置添加班级窗口
    $("#addDialog").dialog({
        title: "添加班级",
        width: 500,
        height: 400,
        iconCls: "icon-add",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'添加',
                plain: true,
                iconCls:'icon-add',
                handler:function(){
                    var validate = $("#addForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                        return;
                    } else{
                        //var gradeid = $("#add_gradeList").combobox("getValue");
                        $.ajax({
                            type: "post",
                            url: "/clazz/add"/*[[@{/clazz/addClazz}]]*/,
                            data: $("#addForm").serialize(),
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //关闭窗口
                                    $("#addDialog").dialog("close");
                                    //清空原表格数据
                                    $("#add_name").textbox('setValue', "");
                                    $("#info").val("");
                                    //重新刷新页面数据
                                    //$('#gradeList').combobox("setValue", gradeid);
                                    $('#dataList').datagrid("reload");

                                } else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;
                                }
                            }
                        });
                    }
                }
            },
            {
                text:'重置',
                plain: true,
                iconCls:'icon-reload',
                handler:function(){
                    $("#add_name").textbox('setValue', "");
                    //重新加载年级
                    $("#info").val("");
                }
            },
        ]
    });


    //搜索按钮监听事件
    $("#search-btn").click(function(){
        $('#dataList').datagrid('load',{
            clazzName: $('#clazzName').val()
        });
    });

    //修改按钮监听事件
    $("#edit-btn").click(function(){
        var selectRow = $("#dataList").datagrid("getSelected");
        //console.log(selectRow);
        if(selectRow == null){
            $.messager.alert("消息提醒", "请选择数据进行修改!", "warning");
            return;
        }
        $("#editDialog").dialog("open");
    });

    //设置编辑班级窗口
    $("#editDialog").dialog({
        title: "编辑班级",
        width: 500,
        height: 400,
        iconCls: "icon-add",
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        draggable: true,
        closed: true,
        buttons: [
            {
                text:'确定修改',
                plain: true,
                iconCls:'icon-add',
                handler:function(){
                    var validate = $("#editForm").form("validate");
                    if(!validate){
                        $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                        return;
                    } else{
                        $.ajax({
                            type: "post",
                            url: "/clazz/update"/*[[@{/clazz/editClazz}]]*/,
                            data: $("#editForm").serialize(),
                            success: function(data){
                                if(data.success){
                                    $.messager.alert("消息提醒",data.message,"info");
                                    //关闭窗口
                                    $("#editDialog").dialog("close");
                                    //清空原表格数据
                                    $("#edit_name").textbox('setValue', "");
                                    $("#edit_info").val("");
                                    //重新刷新页面数据
                                    //$('#gradeList').combobox("setValue", gradeid);
                                    $('#dataList').datagrid("reload");

                                } else{
                                    $.messager.alert("消息提醒",data.message,"warning");
                                    return;
                                }
                            }
                        });
                    }
                }
            },
            {
                text:'重置',
                plain: true,
                iconCls:'icon-reload',
                handler:function(){
                    $("#edit_name").textbox('setValue', "");
                    //重新加载年级
                    $("#edit_info").val("");
                }
            },
        ],
        onBeforeOpen: function(){
            var selectRow = $("#dataList").datagrid("getSelected");
            //console.log(selectRow);
            //设置值
            $("#edit_name").textbox('setValue', selectRow.name);
            $("#edit_info").val(selectRow.info);
            $("#edit-id").val(selectRow.id);
        }
    });
</script>
</body>
</html>